//
//  DebtInfo.m
//  Core
//
//  Created by Cuong Doan on 3/4/13.
//  Copyright (c) 2013 Cuong Doan. All rights reserved.
//

#import "DebtInfo.h"

@interface DebtInfo()

-(void)getDebtByID:(int)userID andOrder:(int)debtOrder;

-(int)getNextOrder:(int)userID;

@end

@implementation DebtInfo
{
    sqlite3 *db;
}

@synthesize USERID, DebtOrder, Description, Amount, AutoFlow, Deadline, isPaid;

-(id)initWithID:(int)userID andOrder:(int)debtOrder
{
    self = [super init];
    
    @try
    {
        [self getDebtByID:userID andOrder:debtOrder];
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }   
    
    return self;
}

-(void)getDebtByID:(int)userID andOrder:(int)debtOrder
{
    sqlite3_stmt *statement;
    
    const char *sql = "select * from Debt where User_ID = ? and DebtOrder = ?";
    
    @try
    {
        [DatabaseManager openAndPrepareDatabase:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:userID andIndex:1];
        
        [DatabaseManager bindInt:statement andValue:debtOrder andIndex:2];
        
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            self.USERID = sqlite3_column_int(statement, 0);
            
            self.DebtOrder = sqlite3_column_int(statement, 1);
            
            self.Description = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 2)];
            
            self.Amount = sqlite3_column_double(statement, 3);
            
            self.Deadline = [DatabaseManager columnDate:statement andIndex:4];
            
            self.AutoFlow = (bool)sqlite3_column_int(statement, 5);
            
            self.isPaid = (bool)sqlite3_column_int(statement, 6);
        }
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }    
}

-(void)insertDebt
{    
    sqlite3_stmt *statement;
    
    const char *sql = "isnert into Debt (User_ID, DebtOrder, Description, Amount, Deadline, AutoFlow, isPaid) values (?, ?, ?, ?, ?, ?, ?)";    
        
    @try
    {
        self.DebtOrder = [self getNextOrder:self.USERID];      
        
        [DatabaseManager openAndPrepareDatabaseV2:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:self.USERID andIndex:1];
        
        [DatabaseManager bindInt:statement andValue:self.DebtOrder andIndex:2];
        
        [DatabaseManager bindText:statement andValue:self.Description andIndex:3];
        
        [DatabaseManager bindDouble:statement andValue:self.Amount andIndex:4];
        
        [DatabaseManager bindDate:statement andValue:self.Deadline andIndex:5];
        
        [DatabaseManager bindInt:statement andValue:(int)self.AutoFlow andIndex:6];
    
        [DatabaseManager bindInt:statement andValue:(int)self.isPaid andIndex:7];
    
        sqlite3_step(statement);
        
        sqlite3_finalize(statement);
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }    
}

-(void)updateDebt
{
    sqlite3_stmt *statement;
    
    const char *sql = "update Debt set Description = ?, Amount = ?, Deadline = ?, AutoFlow = ?, isPaid = ? where User_ID = ? and DebtOrder = ?";    
        
    @try
    {
        [DatabaseManager openAndPrepareDatabaseV2:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindText:statement andValue:self.Description andIndex:1];
        
        [DatabaseManager bindDouble:statement andValue:self.Amount andIndex:2];
        
        [DatabaseManager bindDate:statement andValue:self.Deadline andIndex:3];
        
        [DatabaseManager bindInt:statement andValue:(int)self.AutoFlow andIndex:4];
    
        [DatabaseManager bindInt:statement andValue:(int)self.isPaid andIndex:5];
        
        [DatabaseManager bindInt:statement andValue:self.USERID andIndex:6];
    
        [DatabaseManager bindInt:statement andValue:self.DebtOrder andIndex:7];
    
        sqlite3_step(statement);
        
        sqlite3_finalize(statement);
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }    
}

-(int)getNextOrder:(int)userID
{
    int order;
    
    sqlite3_stmt *statement;
    
    const char *sql = "select (ifnull(max(DebtOrder), 0) + 1) from Debt where User_ID = ?";
    
    @try
    {
        [DatabaseManager prepareDatabase:db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:userID andIndex:1];
        
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            order = sqlite3_column_int(statement, 0);
        }
        
        return order;
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }        
}

@end
